package com.kamikaze;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;



public class Leave {
	String user_id;
	String leaved_date_begin;
	String leaved_date_end;

	String leaved_time_begin;
	
	String leaved_time_end;
	
	String leave_number;
	String checker_id;
	String leave_note;
	int shift_number = 0;
	ArrayList<String> errors = new ArrayList<String>();

	// ArrayList<Leave> Leavedata = new ArrayList<Leave>();

	public Leave() {
	}

	public ArrayList<String> getErrors() {
		return errors;
	}

	public void setErrors(ArrayList<String> errors) {
		this.errors = errors;
	}

	public String getUser_id() {
		return user_id;
	}

	public void setUser_id(String user_id) {
		this.user_id = user_id;
	}

	public String getLeaved_date_begin() {
		return leaved_date_begin;
	}

	public void setLeaved_date_begin(String leaved_date_begin) {
		this.leaved_date_begin = leaved_date_begin;
	}

	public String getLeaved_date_end() {
		return leaved_date_end;
	}

	public void setLeaved_date_end(String leaved_date_end) {
		this.leaved_date_end = leaved_date_end;
	}

	public String getLeaved_time_begin() {
		return leaved_time_begin;
	}

	public void setLeaved_time_begin(String leaved_time_begin) {
		this.leaved_time_begin = leaved_time_begin;
	}

	public String getLeaved_time_end() {
		return leaved_time_end;
	}

	public void setLeaved_time_end(String leaved_time_end) {
		this.leaved_time_end = leaved_time_end;
	}

	public String getLeave_number() {
		return leave_number;
	}

	public void setLeave_number(String leave_number) {
		this.leave_number = leave_number;
	}

	public String getChecker_id() {
		return checker_id;
	}

	public void setChecker_id(String checker_id) {
		this.checker_id = checker_id;
	}

	public String getLeave_note() {
		return leave_note;
	}

	public void setLeave_note(String leave_note) {
		this.leave_note = leave_note;
	}

	public int getShift_number(String user_id, String leaved_date_begin) {

		String year = leaved_date_begin.substring(0, 4);
		String month = leaved_date_begin.substring(5, 7);
		String day = leaved_date_begin.substring(8);
		String beginday = year + month + day;
		System.out.print(beginday);
		String get_sfNo = "select `shift_number` from `shift` where `user_id`=? and date(`shift_date`)=?";

		// 取得shift_number
		try {
			Class.forName("com.mysql.jdbc.Driver");
			Connection con = DriverManager
					.getConnection("jdbc:mysql://j.snpy.org/j2?user=team2&password=kamikaze&useUnicode=true&characterEncoding=UTF-8");
			PreparedStatement pstat = con.prepareStatement(get_sfNo);
			pstat.setString(1, user_id);
			pstat.setString(2, beginday);
			ResultSet rs = pstat.executeQuery();
			if (rs.next()) {
				shift_number = rs.getInt("shift_number");

				System.out.println("java shift_number" + shift_number);
			} else {
				errors.add("你可能是隻菜鳥,因為你沒班表");
			}
			rs.close();
			pstat.close();
			con.close();
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return shift_number;
	}

	public void setShift_number(int shift_number) {
		this.shift_number = shift_number;
	}

	public boolean process(String user_id, String leaved_date_begin,
			String leaved_date_end, String leaved_time_begin,
			String leaved_time_end, String leave_number, String checker_id,
			String leave_note, int shift_number) {
		boolean succ = false;
		int st_number=0;
		String year_b = leaved_date_begin.substring(0, 4);
		String month_b = leaved_date_begin.substring(5, 7);
		String day_b = leaved_date_begin.substring(8);
		String beginday = year_b + month_b + day_b;
		String insert_leave = "INSERT INTO `j2`.`leavedata` "
				+ "( `shift_number`, `leaved_date`, `leaved_time_begin`, `leaved_time_end`, `leave_number`, `checker_id`,  `leave_note`) "
				+ "VALUES (?,?,?,?,?,?,?);";
		
		// 寫入leavedata
		// 判斷只請一天或多天
		if (leaved_date_begin.equals(leaved_date_end)) {

			Connection con1;
			try {
				Class.forName("com.mysql.jdbc.Driver");
				con1 = DriverManager
						.getConnection("jdbc:mysql://j.snpy.org/j2?user=team2&password=kamikaze&useUnicode=true&characterEncoding=UTF-8");
				PreparedStatement pstat2 = con1.prepareStatement(insert_leave);
				pstat2.setInt(1, shift_number);
				pstat2.setString(2, beginday);
				pstat2.setString(3, leaved_time_begin);
				pstat2.setString(4, leaved_time_end);
				pstat2.setString(5, leave_number);
				pstat2.setString(6, checker_id);
				pstat2.setString(7, leave_note);
				int row = pstat2.executeUpdate();
				System.out.println("增加" + row + "筆資料");
				if (row != 0) {
					succ = true;errors.add("新增成功");
				}
				pstat2.close();
				con1.close();
			} catch (SQLException e) {
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}
		}
		// 一次請多天假
		else {	
			int dff_days = 0;
			DateFormat   f   =   new   SimpleDateFormat( "yyyy-MM-dd"); 
				try {
				Calendar cal = Calendar.getInstance();	
				Date p = f.parse( leaved_date_begin );
				cal.setTime(p);
				cal.add(Calendar.DAY_OF_YEAR,-1);
				Date b = f.parse( leaved_date_begin );
				Date e = f.parse( leaved_date_end ) ;
				int diff = (int) (e.getTime()-b.getTime());
				System.out.println("Difference is " + (diff/(1000*60*60*24)) + " days."); 
				dff_days = (diff/(1000*60*60*24));
			
				for(int i = 0;i<=dff_days;i++){		
					Date j = cal.getTime();
					System.out.println("Now Set Date = " +f.format(j));
				    cal.add(Calendar.DAY_OF_YEAR,1);
				    j = cal.getTime();
				    System.out.println("increase"+ i+" Days = " + f.format(j));
					String lday=(String) f.format(j);
					Class.forName("com.mysql.jdbc.Driver");
					Connection con3 = DriverManager
							.getConnection("jdbc:mysql://j.snpy.org/j2?user=team2&password=kamikaze&useUnicode=true&characterEncoding=UTF-8");
//取得每天的shift_number
					String get_sfNo2 = "select `shift_number` from `shift` where `user_id`=? and date(`shift_date`)=?";
					PreparedStatement pstat6 = con3.prepareStatement(get_sfNo2);
					pstat6.setString(1, user_id);
					pstat6.setString(2, lday);
					ResultSet rs111 = pstat6.executeQuery();
					if (rs111.next()) {
						st_number = rs111.getInt("shift_number");}
					else{succ=false;errors.add("請假日期有誤");}
					rs111.close();
					pstat6.close();
					con3.close();
					Connection con4 = DriverManager
							.getConnection("jdbc:mysql://j.snpy.org/j2?user=team2&password=kamikaze&useUnicode=true&characterEncoding=UTF-8");
					PreparedStatement pstat7 = con4.prepareStatement(insert_leave);
					pstat7.setInt(1, st_number);
					pstat7.setString(2, lday);
					pstat7.setString(3, leaved_time_begin);
					pstat7.setString(4, leaved_time_end);
					pstat7.setString(5, leave_number);
					pstat7.setString(6, checker_id);
					pstat7.setString(7, leave_note);
					int row = pstat7.executeUpdate();
					System.out.println("增加" + row + "筆資料");
					if (row != 0) {
						succ = true;
					}
					pstat7.close();
					con4.close();		
				}

				} catch (ParseException e1) {e1.printStackTrace();} 
				catch (SQLException e1) {e1.printStackTrace();} 
				catch (ClassNotFoundException e1) {e1.printStackTrace();}
	
			
		}

		return succ;
	}

	// 確認該日期是否請過假
	public boolean check(int shift_number,String leaved_date_begin,String leaved_date_end) {
		boolean pass = true;
		String year_b = leaved_date_begin.substring(0, 4);
		String month_b = leaved_date_begin.substring(5, 7);
//		String day_b = leaved_date_begin.substring(8);
		String year_e = leaved_date_end.substring(0, 4);
		String month_e = leaved_date_end.substring(5, 7);
//		String day_e = leaved_date_end.substring(8);
//		String beginday = year_b + month_b + day_b;
		Date date = new Date();
		DateFormat df = new SimpleDateFormat("yyyy-MM");
		String now_date = df.format(date);
		String year_now = now_date.substring(0, 4);
		String month_now = now_date.substring(5, 7);
		
		String check_bd = "select * from leavedata where shift_number = ?";
		if(!year_b.equals(year_now)){pass=false;errors.add("只能在今年請假");}
		if(!year_e.equals(year_now)){pass=false;errors.add("只能在今年請假");}
		if(!month_b.equals(month_now)){pass=false;errors.add("只能在本月請假");}
		if(!month_e.equals(month_now)){pass=false;errors.add("只能在本月請假");}
		else{
			try {
				Class.forName("com.mysql.jdbc.Driver");
				Connection con2 = DriverManager
						.getConnection("jdbc:mysql://j.snpy.org/j2?user=team2&password=kamikaze&useUnicode=true&characterEncoding=UTF-8");
				PreparedStatement pstat3 = con2.prepareStatement(check_bd);
				pstat3.setInt(1, shift_number);
				ResultSet rsc = pstat3.executeQuery();
				if (rsc.next()) {
					pass = false;
					errors.add("請假日期重覆");
				}
				rsc.close();
				pstat3.close();
				con2.close();
			} catch (ClassNotFoundException e1) {e1.printStackTrace();
			} catch (SQLException e) {e.printStackTrace();}
		}
		
//判斷多天請假是否同年同月請假
//	if (!leaved_date_begin.equals(leaved_date_end)) {
//		if (year_b.equals(year_e) & month_b.equals(month_e)) {
//			pass=true;
//		} else {
//			pass = false;
//			errors.add("請不要亂搞");
//		}
//	}
		return pass;

	}

}
